package com.gee.spring.boot.influxdb.core.query.segment;

import com.gee.spring.boot.influxdb.core.enums.AndOr;

import java.util.ArrayList;
import java.util.stream.Collectors;

import static com.gee.spring.boot.influxdb.core.constant.StrConstant.*;

/**
 * @author gepengjun
 * @since 2024/4/28 8:45
 */
public class ConditionSegmentList extends ArrayList<IConditionSegment> implements IConditionSegment {

    private static final long serialVersionUID = 248089647925916909L;

    private AndOr andOr;

    public void setAndOr(AndOr andOr) {
        this.andOr = andOr;
    }

    @Override
    public String getSqlSegment() {
        if (size() > 0){
            get(0).setAndOr(null);
        }
        StringBuilder sqlSegment = new StringBuilder();
        if (andOr != null){
            sqlSegment.append(andOr.getKeyWord()).append(LEFT_BRACKET);
        }
        sqlSegment.append(this.stream().map(IConditionSegment::getSqlSegment).collect(Collectors.joining(SPACE)));
        if (andOr != null){
            sqlSegment.append(RIGHT_BRACKET);
        }
        return sqlSegment.toString();
    }
}
